﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers.world {
    import flash.events.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.models.world.*;

    public class BoostMachineCommand extends Command {

        public var event:InfoEvent;
        public var energyModel:EnergyModel;
        public var tickModel:TickModel;
        public var worldModel:WorldModel;
        public var userModel:UserModel;

        override public function execute():void{
            var _local1:Object = {taskType:GameConstants.BOOST_MACHINE, itemType:GameConstants.UPPERCASE_ENERGY, onResult:this.onResult};
            var _local2:Array = [new RequestVO("boost_treatment", {id:this.event.id})];
            var _local3:TaskRequestEvent = new TaskRequestEvent(EventConstants.SEND_TASK_REQUEST, _local2, _local1);
            dispatch(_local3);
            this.energyModel.pendingCallsToBackendCount++;
            dispatch(new UserModelEvent(UserModelEvent.ON_MODEL_CHANGED));
            if (this.energyModel.energy > 0){
                this.track();
            };
        }
        private function onResult(_arg1:ResultVO):void{
            var _local2:Object;
            var _local3:Object;
            var _local4:Object;
            var _local5:ITreatable;
            var _local6:ITreatment;
            var _local7:String;
            if (_arg1.success){
                _local2 = _arg1.data.boost_treatment;
                this.energyModel.pendingCallsToBackendCount--;
                _local3 = _local2.treatment;
                _local4 = _local2.energy;
                _local5 = this.worldModel.getPatientByTreatmentId(this.event.id);
                _local6 = _local5.currentTreatment;
                this.energyModel.energyStartedAt = _local4.started_at;
                this.energyModel.energyUsed = _local4.used;
                this.energyModel.energyMax = _local4.max_value;
                this.energyModel.energyRefillDuration = _local4.refill_duration;
                this.energyModel.energyExtra = _local4.extra;
                this.userModel.numberOfUsedEnergy = _local2.energy_used_count;
                dispatch(new Event(EventConstants.UPDATE_ACHIEVEMENTS));
                this.energyModel.startDifferenceCounter();
                if (_local5.currentTreatment.id != _local3[ProtocolConstants.ID]){
                    l("f boosted treatment and treatment response incorrect");
                } else {
                    _local5.currentTreatment.finishedAt = _local3[ProtocolConstants.FINISHED_AT];
                    _local5.currentTreatment.finishedIn = _local3[ProtocolConstants.FINISHED_IN];
                };
                if (_local5.state != GameConstants.STATE_BABY){
                    _local7 = ((_local6.finishedIn > 0)) ? EventConstants.ON_TREATMENT_STARTED : EventConstants.ON_TREATMENT_FINISHED;
                    dispatch(new InfoEvent(EventConstants.TREATMENT_STATUS_UPDATED, _local7, _local6));
                };
            };
        }
        private function track():void{
            var _local2:Object;
            var _local1:ITreatable = this.worldModel.getPatientByTreatmentId(this.event.id);
            if (_local1){
                _local2 = new Object();
                _local2.type = GameConstants.ENERGY;
                _local2.st1 = "use";
                _local2.st2 = _local1.type;
                dispatch(new TrackingEvent(TrackingConst.TRACK, TrackingConst.EVENT, _local2));
            };
        }

    }
}//package net.wooga.pets.controllers.world 
